package com.t282.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t282.crm.entity.CstService;
import com.t282.crm.service.impl.CstServiceServiceImpl;
import com.t282.crm.vo.CustomerQueryVo;
import com.t282.crm.vo.ServiceQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api("服务管理")
@RestController
@RequestMapping("/api/cstService")
public class ServiceController {
    @Autowired
    private CstServiceServiceImpl cstServiceService;

    @ApiOperation(value = "添加服务")
    @PostMapping("/cstServiceAdd")
    public Map ServiceAdd(CstService cstService){
        Map map = new HashMap();
        boolean save = cstServiceService.save(cstService);
        map.put("result",true);
        map.put("message","保存（新增）成功");
        return map;
    }

    @ApiOperation(value = "服务查询，进行分页")
    @PostMapping("/findByServiceList/{index}/{limit}")
    public Map findByServiceList(@PathVariable Integer index, @PathVariable Integer limit, ServiceQueryVo serviceQueryVo){
        Map map = new HashMap();
        //分页
        Page<CstService> page = new Page<>(index,limit);
        //条件
        QueryWrapper<CstService> wrapper = new QueryWrapper<>();
        //判断条件是否为空
        if (null != serviceQueryVo.getSvrCustName() && !"".equals(serviceQueryVo.getSvrCustName())){
            wrapper.like("svr_cust_name",serviceQueryVo.getSvrCustName());
        }
        if (null != serviceQueryVo.getSvrTitle() && !"".equals(serviceQueryVo.getSvrTitle())){
            wrapper.like("svr_title",serviceQueryVo.getSvrTitle());
        }
        if (null != serviceQueryVo.getSvrType() && !"".equals(serviceQueryVo.getSvrType())){
            wrapper.like("svr_type",serviceQueryVo.getSvrType());
        }

        Page<CstService> pages = cstServiceService.page(page,wrapper);
        map.put("return",true);
        map.put("message","成功");
        map.put("date",pages);

        return map;
    }


    @ApiOperation(value = "服务删除")
    @PostMapping("/ServiceDel/{id}")
    public Map ServiceDel(@PathVariable Integer id){
        Map map = new HashMap();
        cstServiceService.removeById(id);
        map.put("result",true);
        map.put("message","删除成功");
        return map;
    }

    @ApiOperation(value = "服务分配")
    @PostMapping("/ServiceAssign")
    public Map ServiceAssign(CstService cstService){
        Map map = new HashMap();
        cstServiceService.updateById(cstService);
        map.put("result",true);
        map.put("message","服务分配成功");
        return map;
    }

}
